<template>
  <el-card class="mt-20">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddAttr"
      >添加属性</el-button
    >
    <el-table :data="attrList" class="mt-10" border>
      <el-table-column label="序号" width="60" type="index" align="center" />
      <el-table-column prop="attrName" label="属性名称" width="180" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            class="attr-tag"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot="{ row }">
          <el-tooltip effect="light" content="修改属性" placement="bottom">
            <el-button
              type="primary"
              :icon="Edit"
              size="small"
              @click="showUpdateAttr(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip effect="light" content="删除属性" placement="bottom">
            <span>
              <el-popconfirm
                :title="`你确定要删除${row.valueName}吗？`"
                @confirm="delAttr(row.id)"
              >
                <template #reference>
                  <el-button :icon="Delete" type="danger" size="small" />
                </template>
              </el-popconfirm>
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
import { getAttrListApi, delAttrApi } from "@/api/product/attr";
import { useAttrStore } from "@/stores/attr";
import { ref, watch } from "vue";

import type { AttrList, AttrItem } from "@/api/product/model/attrModel";
import { ElMessage } from "element-plus";

const categoryStore = useCategoryStore();

const attrStore = useAttrStore();

const emit = defineEmits(["setIsShowAttrList"]);

// 进入添加属性
const showAddAttr = () => {
  //通过调用 store 的 $reset() 方法将 state 重置为初始值
  //这里是通过点击修改属性按钮进入后按取消再点击添加属性按钮将内容重置为空(初始值)
  attrStore.$reset();
  emit("setIsShowAttrList", false);
};

const attrList = ref<AttrList>([]);

//点击修改属性
const showUpdateAttr = (row: AttrItem) => {
  // .$patch 更改属性
  attrStore.$patch({
    id: row.id,
    attrName: row.attrName,
    attrValueList: row.attrValueList,
  });
  emit("setIsShowAttrList", false);
};
//封装复用获取品牌列表
const getAttrList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;

  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

watch(
  () => categoryStore.category3Id,
  async (category3Id) => {
    if (!category3Id) {
      attrList.value = [];
      return;
    }

    getAttrList();
  },
  { immediate: true }
);

//删除属性
const delAttr = async (id: number) => {
  await delAttrApi(id);
  ElMessage.success("删除属性成功");
  getAttrList();
};
</script>

<style scoped>
.attr-tag {
  margin-right: 10px;
}
</style>
